<?php 
//header('Content-type: image/png');
error_reporting(0);
$const = $_GET['const'];
$std = $_GET['std'];
$tow = $_GET['tow'];
//$tanggal = unserialize($_GET['tanggal']);
$tanggal = unserialize($_GET['realdate']);
$tinggi = unserialize($_GET['tinggi']);
$pemeriksaan = $_GET['pemeriksaan'];
$edd = $_GET['edd'];

include("pChart/class/pDraw.class.php");  
include("pChart/class/pImage.class.php");  
include("pChart/class/pData.class.php"); 

function proporsi($ga)
{
	//dalam presentasi
	return 299.1 - 31.85 * $ga + 1.094*$ga*$ga - 0.01055*$ga*$ga*$ga;
}

//Perhitungan
//$cv = $std*3000/$const;
$MyData = new pData();  
$index = 0;
for ($i=24;$i<=42;$i++)
{
	$down[] = $i;
	$down2[] = $edd;
	$curva_tow[$index] = proporsi($i)*$tow/100;
	$curva_tow90[$index] = $curva_tow[$index]*1.14;//((1.28 * $cv)*$curva_tow[$index]/100);
	$curva_tow10[$index] = $curva_tow[$index]*0.86;// - ((1.28 * $cv)*$curva_tow[$index]/100);
	$index2 = $index * 3;
	$fh_axis[$index2] = ($curva_tow[$index]+5012)/226;
	$fh_axis[$index2+1] = ($curva_tow90[$index]+5012)/226;
	$fh_axis[$index2+2] = ($curva_tow10[$index]+5012)/226;
	$index++;
}

/* Prepare some nice data & axis config */
$MyData->addPoints($curva_tow,"tow50");
$MyData->addPoints($curva_tow10,"tow10");
$MyData->addPoints($curva_tow90,"tow90");
//$MyData->addPoints($curva_tow90,"zz");
$MyData->setSerieOnAxis("tow50",1);
$MyData->setSerieOnAxis("tow90",1);
$MyData->setSerieOnAxis("tow10",1);
//$MyData->setSerieOnAxis("zz",0);

/*for ($i=1;$i<=pemeriksaan;$i++){
	$MyData->addPoints($tinggi[$i], "pertumbuhan");
	$MyData->setSerieWeight("pertumbuhan",3);
	$MyData->setSerieOnAxis("pertumbuhan",1);
}*/

//$MyData->addPoints(array(33,42,45,38,35,25),"Probe 1"); 
//$MyData->addPoints(array(22,27,25,38,39,42),"Probe 2"); 
//$MyData->setSerieWeight("pertumbuhan",3);
//$MyData->setSerieWeight("Probe 1",2); 
//$MyData->setSerieTicks("Probe 2",4); 


$MyData->setAxisPosition(1,AXIS_POSITION_RIGHT);
$MyData->setAxisName(0,"Tinggi Fundus");
$MyData->setAxisName(1,"Perkiraan Berat");
$MyData->setSerieOnAxis(" Values",0);
 
/* Bind a data serie to the X axis */
$MyData->addPoints($down,"Labels");
$MyData->setSerieDescription("Labels","My labels");
$MyData->setAbscissa("Labels");

//yData->addPoints($down2,"Labels 2");
//$MyData->setSerieDescription("Labels 2","My labels 2");
//$MyData->setAbscissa("Labels 2");

 
/* Create a pChart object and associate your dataset */  
$myPicture = new pImage(800,800,$MyData); 
$myPicture->setFontProperties(array("FontName"=>"pChart/fonts/Forgotte.ttf","FontSize"=>11));
 
/* Define the graph area and do some makeup */
$myPicture->setGraphArea(80,60,700,650);
$myPicture->drawText(400,55,"Grafik Pertumbuhan Janin",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));
$myPicture->drawFilledRectangle(80,60,700,650,array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>5));

/* Compute and draw the scale */
//$myPicture->drawScale(array("DrawYLines"=>array(0)));

$AxisBoundaries = array(0=>array("Min"=>22.177,"Max"=>44.3),1=>array("Min"=>0,"Max"=>5000));
//$AxisBoundaries = array(0=>array("Min"=>-20,"Max"=>44.3),1=>array("Min"=>0,"Max"=>5000));

$myPicture->drawScale(array("CycleBackground"=>TRUE,"GridTicks"=>3,"DrawSubTicks"=>TRUE,"DrawArrows"=>TRUE,"ArrowSize"=>6,"Mode"=>SCALE_MODE_MANUAL,"ManualScale"=>$AxisBoundaries));
$myPicture->drawSplineChart(); 
//$myPicture->drawPlotChart(array("PlotBorder"=>TRUE,"BorderSize"=>1,"Surrounding"=>-60,"BorderAlpha"=>80)); 

/*---------------------------------------------------*/

$MyData = new pData();  

$awal = mktime(0,0,0,substr($edd,5,2),substr($edd,8,2)-(16*7),substr($edd,0,4));
//$tgl_awal = new DateTime(date("Y-m-d",mktime(0,0,0,substr($edd,5,2),substr($edd,8,2)-(16*7),substr($edd,0,4))));



for ($i=24;$i<=42;$i++) $tinggi_sparse[] = VOID; 
for ($i=0;$i<sizeof($tanggal);++$i){
	$tgl = mktime(0,0,0,substr($tanggal[$i],5,2),substr($tanggal[$i],8,2),substr($tanggal[$i],0,4));
	$selisih_minggu = round(($tgl-$awal)/604800);
	$tinggi_sparse[$selisih_minggu] = $tinggi[$i];
}

$MyData->addPoints($tinggi_sparse,"pertumbuhan");
$MyData->setSerieTicks("pertumbuhan",4);

$MyData->setAxisPosition(1,AXIS_POSITION_RIGHT);
$MyData->setAxisName(0,"Tinggi Fundus");
$MyData->setAxisName(1,"Perkiraan Berat");
$MyData->setSerieOnAxis(" Values",0);
 
/* Bind a data serie to the X axis */
$MyData->addPoints($down,"Labels");
$MyData->setSerieDescription("Labels","My labels");
$MyData->setAbscissa("Labels");

$myPicture->setDataSet($MyData);
$myPicture->setFontProperties(array("FontName"=>"pChart/fonts/Forgotte.ttf","FontSize"=>11));
 
/* Define the graph area and do some makeup */
$myPicture->setGraphArea(80,60,700,650);
//$myPicture->drawText(400,55,"Grafik Pertumbuhan Janin",array("FontSize"=>20,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));
$myPicture->drawFilledRectangle(80,60,700,650,array("R"=>255,"G"=>255,"B"=>255,"Surrounding"=>-200,"Alpha"=>5));



/* Compute and draw the scale */
//$myPicture->drawScale(array("DrawYLines"=>array(0)));

$AxisBoundaries = array(0=>array("Min"=>22.177,"Max"=>44.3),1=>array("Min"=>0,"Max"=>5000));
//$AxisBoundaries = array(0=>array("Min"=>-20,"Max"=>44.3),1=>array("Min"=>0,"Max"=>5000));

$myPicture->drawScale(array("CycleBackground"=>TRUE,"GridTicks"=>3,"DrawSubTicks"=>TRUE,"DrawArrows"=>TRUE,"ArrowSize"=>6,"Mode"=>SCALE_MODE_MANUAL,"ManualScale"=>$AxisBoundaries));
$myPicture->drawSplineChart(array("BreakVoid"=>FALSE)); 
$myPicture->drawPlotChart(array("PlotBorder"=>TRUE,"BorderSize"=>1,"Surrounding"=>-60,"BorderAlpha"=>80)); 


$myPicture->drawText(350,680, "Minggu Kehamilan");
$myPicture->drawText(70,680, "".date("l",$awal));

for($i=24; $i<=42; $i++){
	$tanggal = mktime(0,0,0,substr($edd,5,2),substr($edd,8,2)-((40-$i)*7),substr($edd,0,4));
	$myPicture->drawText(-705+$i*33,730, "".date(" d\n M\no",$tanggal));
}


$myPicture->autoOutput("make.png");
?>
